/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mygameframework.isoengine;

import com.mygameframework.util.Utility;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Dictionary;

/**
 *
 * @author bill
 */
public class MouseMapper {
    private int tw;
    private int th;
    private float halfOfTw;
    private float halfOfTh;
    private Point origin;
    private Scroller scroller;
    private TileWalker walker;
//    private TilePlotter plotter;
    protected float slope;
    private float b;//截距

    public MouseMapper() {
    }
    
    public MouseMapper(TileWalker wkr,Scroller scr,int tw,int th) {
        this.walker=wkr;
        this.scroller=scr;
        this.tw=tw;
        this.th=th;
        slope=th/(float)tw;
        halfOfTw=(tw/2.0f);
        halfOfTh=(th/2.0f);
        b=th/2f;
    }
    /**
     * 只适用于isometric
     * @param finePoint
     * @return 
     */
    public Direction fineDirection(Point finePoint){
        int x=finePoint.x;
        int y=finePoint.y;
//        System.out.println(halfOfTw+" "+halfOfTh);
        //左上
        if(halfOfTw >x && halfOfTh> y && b>y- (-slope)*x){
            return Direction.NORTHWEST;
        }
        //右上
        if(halfOfTw <x && halfOfTh> y && -b > y- (slope)*x){
            return Direction.NORTHEAST;
        }
//        System.out.println("half tw "+halfOfTw);
//        System.out.println("half th "+halfOfTh );
//        System.out.println(" x y "+x+" "+y);
        //左下
        if(halfOfTw >x && halfOfTh< y && b < y- (slope)*x){
            return Direction.SOUTHWEST;
        }
        //右下 
        if(halfOfTw <x && halfOfTh< y && b*3 < y- (-slope)*x){
            return Direction.SOUTHEAST;
        }
        return Direction.CENTER;
    }
    /***
     * screen to map
     * @param ptMouse 屏幕的鼠标点
     * @return 
     */
    public Point mapMouse(Point ptMouse){
        ptMouse=scroller.screenToWorld(ptMouse);//screen to world ,get a worldspace point
        /*因为是下面的步行是以原点为起点的，所以这里的世界坐标要减去原点坐标，使之成为相对于原点的坐标
         * 一般情况下,screenToWorld返回的点是相对于worldspace左上角的,即原点,但在
         * diamond贴图中,原点坐标在worldspace中上
        */
        ptMouse.x-=origin.x;//relative to origin point 
        ptMouse.y-=origin.y;//origin就是Map坐标00对应的world坐标
        Point ptMouseMapCoarse = new Point();
        ptMouseMapCoarse.x = ptMouse.x / tw;
        ptMouseMapCoarse.y = ptMouse.y / th;
        
        Point ptMouseMapFine = new Point();
        ptMouseMapFine.x = ptMouse.x % tw;
        ptMouseMapFine.y = ptMouse.y % th;
        if (ptMouseMapFine.x < 0) { //余数小于0
            ptMouseMapFine.x += tw;
            ptMouseMapCoarse.x--;
        }
        if (ptMouseMapFine.y < 0) {//余数小于0
            ptMouseMapFine.y += th; 
            ptMouseMapCoarse.y--;
        }
        Point ptMap = new Point(0, 0);
        //粗坐标计算
        while (ptMouseMapCoarse.y > 0) {
            ptMouseMapCoarse.y--;
            ptMap=walker.tileWalk(ptMap, Direction.SOUTH);
        }
        while (ptMouseMapCoarse.y < 0) {
            ptMouseMapCoarse.y++;
            ptMap = walker.tileWalk(ptMap, Direction.NORTH);
        }
        while (ptMouseMapCoarse.x > 0) {
            ptMouseMapCoarse.x--;
            ptMap = walker.tileWalk(ptMap, Direction.EAST);
        }
        while (ptMouseMapCoarse.x < 0) {
            ptMouseMapCoarse.x++;
            ptMap = walker.tileWalk(ptMap, Direction.WEST);
        }
//        System.out.println("fine "+ptMouseMapFine);
        //精坐标计算
        //针对矩形tile
        if(walker.getMapType()==MapType.MAP_RECTANGULAR){
            if(ptMouseMapFine.x!=0)
                walker.tileWalk(ptMap, Direction.EAST);
            if(ptMouseMapFine.y!=0)
                walker.tileWalk(ptMap, Direction.SOUTH);
            return ptMap;
        }
        //精坐标计算
        //针对凌形tile
        Direction d=fineDirection(ptMouseMapFine);
//        System.out.println(d);
        switch (d) {
            case CENTER:
                //DO NOTHING
                break;
            case NORTHWEST:
                ptMap=walker.tileWalk(ptMap, Direction.NORTHWEST);
                break;
            case NORTHEAST:
                ptMap=walker.tileWalk(ptMap, Direction.NORTHEAST);
                break;
            case SOUTHEAST:
                ptMap=walker.tileWalk(ptMap, Direction.SOUTHEAST);
                break;
            case SOUTHWEST:
                ptMap=walker.tileWalk(ptMap, Direction.SOUTHWEST);
                break;
            default:
                throw new AssertionError();
        }//end switch
        return ptMap;         
    }
    /**
     * 
     * @param plotter
     * @param dx 背景地砖的TileSet.dstRect.x
     * @param dy 背景地砖的TileSet.dstRect.y
     */
    public void calcOriginPoint(TilePlotter plotter,int dx,int dy){
        origin=plotter.plotTile(new Point(0,0));
        origin.x+=dx;
        origin.y+=dy;
    }
    public Point getOrigin() {
        return origin;
    }

    public void setOrigin(Point origin) {
        this.origin = origin;
    }

    public Scroller getScroller() {
        return scroller;
    }

    public void setScroller(Scroller scroller) {
        this.scroller = scroller;
    }

    public TileWalker getWalker() {
        return walker;
    }

    public void setWalker(TileWalker walker) {
        this.walker = walker;
    }
    public int getTileWidth(){
        return tw;
    }
    public void setTileWidth(int w){
        tw=w;
    }
    public int getTileHeight(){
        return th;
    }
    public void setTileHeight(int h){
        this.th=h;
    }
    
    //不用
    public class MouseMap{
        Dimension m;//存了宽 高 五色图的
        Point refPt;//MAP(0,0) 对应的ptPlot
        Direction[] mouseMapLookup;
    }
    //不用
    public void loadMouseMap(MouseMap mmp,String path) throws IOException{
        BufferedImage img=Utility.loadBufferedImage(path);
        int w=img.getWidth();
        int h=img.getHeight();
        mmp.m=new Dimension(w, h);
        mmp.mouseMapLookup=new Direction[w*h];
        int crNW=img.getRGB(0, 0);
        int crNE=img.getRGB(w-1, 0);
        int crSE=img.getRGB(w-1, h-1);
        int crSW=img.getRGB(0, h-1);
        int crtest;
        for(int y=0;y<h;y++){
            for(int x=0;x<w;x++){
                crtest=img.getRGB(x, y);
                mmp.mouseMapLookup[x+w*y]=Direction.CENTER;

                if(crtest ==crNW){
                    mmp.mouseMapLookup[x+w*y]=Direction.NORTHWEST;
                }
                if (crtest == crNE) {
                    mmp.mouseMapLookup[x + y *w] =Direction.NORTHEAST;
                }
                if (crtest == crSW) {
                    mmp.mouseMapLookup[x + y *w] =Direction.SOUTHWEST;
                }
                if (crtest == crSE) {
                    mmp.mouseMapLookup[x + y *w] =Direction.SOUTHEAST;
                }
                
            }
        }
        
        
    }
}
